/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.zhengjie.suli.location.rest;

import me.zhengjie.annotation.Log;
import me.zhengjie.suli.location.domain.Location;
import me.zhengjie.suli.location.service.LocationService;
import me.zhengjie.suli.location.service.dto.LocationQueryCriteria;
import me.zhengjie.suli.location.service.impl.LocationServiceImpl;
import org.springframework.data.domain.Pageable;
import lombok.RequiredArgsConstructor;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import io.swagger.annotations.*;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://eladmin.vip
* @author WYH
* @date 2024-02-05
**/
@RestController
@RequiredArgsConstructor
@Api(tags = "库位管理")
@RequestMapping("/api/location")
public class LocationController {

    private final LocationService locationService;

    @Log("导出数据")
    @ApiOperation("导出数据")
    @GetMapping(value = "/download")
    @PreAuthorize("@el.check('location:list')")
    public void exportLocation(HttpServletResponse response, LocationQueryCriteria criteria) throws IOException {
        locationService.download(locationService.queryAll(criteria), response);
    }

    @GetMapping
    @Log("查询库位")
    @ApiOperation("查询库位")
    @PreAuthorize("@el.check('location:list')")
    public ResponseEntity<Object> queryLocation(LocationQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(locationService.queryAll(criteria,pageable),HttpStatus.OK);
    }

    @PostMapping
    @Log("新增库位")
    @ApiOperation("新增库位")
    @PreAuthorize("@el.check('location:add')")
    public ResponseEntity<Object> createLocation(@Validated @RequestBody Location resources){
        return new ResponseEntity<>(locationService.create(resources),HttpStatus.CREATED);
    }

    @PutMapping
    @Log("修改库位")
    @ApiOperation("修改库位")
    @PreAuthorize("@el.check('location:edit')")
    public ResponseEntity<Object> updateLocation(@Validated @RequestBody Location resources){
        locationService.update(resources);
        return new ResponseEntity<>(HttpStatus.NO_CONTENT);
    }

    @DeleteMapping
    @Log("删除库位")
    @ApiOperation("删除库位")
    @PreAuthorize("@el.check('location:del')")
    public ResponseEntity<Object> deleteLocation(@RequestBody Long[] ids) {
        locationService.deleteAll(ids);
        return new ResponseEntity<>(HttpStatus.OK);
    }

    @PostMapping("/getLocationUsage")
    @ApiOperation("查询库位用量")
    public ResponseEntity<Object> getLocationUsage(@RequestBody Map<String, String> paramMap){
        List<List<Map<String, Object>>> result = locationService.getLocationUsage(paramMap);
        return new ResponseEntity(result, HttpStatus.OK);
    }
}